<?php 
/*
function update_menu_files () {
	$result = 1;
	
	$current_menu = file_get_contents("menu.txt");
	file_put_contents("menu_history.txt", "\r\n{$current_menu}",FILE_APPEND);
	$current_navbar = file_get_contents("navbar.txt");
	file_put_contents("navbar_history.txt", "\r\n{$current_navbar}",FILE_APPEND);
	
	
	
	
	rename("pending_menu.txt","menu.txt");
	rename("pending_navbar.txt","navbar.txt");
	if ((file_exists("pending_menu.txt"))||(file_exists("pending_menu.txt"))) $result = -1;
	return $result;
}

$config_module = "{$_SERVER["DOCUMENT_ROOT"]}/modules/config/index.php";
include "$config_module";

$error = false;


if (is_dir("{$_SERVER["DOCUMENT_ROOT"]}/modules/menu")) {
	chdir("{$_SERVER["DOCUMENT_ROOT"]}/modules/menu");
	if (! update_menu_files ()) $error = true;
}


if (isset($config["template"]) && (is_dir("{$_SERVER["DOCUMENT_ROOT"]}/modules/templates/{$config["template"]}/menu"))) {
	chdir("{$_SERVER["DOCUMENT_ROOT"]}/modules/templates/${config["template"]}/menu");
	if (! update_menu_files ()==-1) $error = true;
}

if (! $error) echo "Menus have been published";
else  echo "There has been a problem with publishing the menus";
*/

$menu = "{$_SERVER["DOCUMENT_ROOT"]}/modules/menu";
$archive = "{$_SERVER["DOCUMENT_ROOT"]}/modules/menu/archive";
$menu = "{$_SERVER["DOCUMENT_ROOT"]}/modules/menu/menu.json";
$navbar = "{$_SERVER["DOCUMENT_ROOT"]}/modules/menu/navbar.json";
$pending_menu = "{$_SERVER["DOCUMENT_ROOT"]}/modules/menu/pending_menu.json";
$pending_navbar = "{$_SERVER["DOCUMENT_ROOT"]}/modules/menu/pending_navbar.json";



if ((!file_exists($pending_menu))&&(!file_exists($pending_navbar))) {
    echo "No menus to publish";
} else {
    $ref = $today = date("Ymd_His");
    copy("$menu", "{$archive}/{$ref}_menu.json");
    copy("$navbar", "{$archive}/{$ref}_navbar.json");
    
    if (file_exists("{$archive}/{$ref}_menu.json")) {
        copy ("$pending_menu","$menu");
        unlink("$pending_menu");
    }
    if (file_exists("{$archive}/{$ref}_navbar.json")) {
        copy ("$pending_navbar","$navbar");
        unlink("$pending_navbar");
    }
     
    echo "menus published";
}

?>